package com.quanfaqiang.demo1.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quanfaqiang.demo1.anno.AddLog;
import com.quanfaqiang.demo1.anno.DeleteLog;
import com.quanfaqiang.demo1.anno.UpdateLog;
import com.quanfaqiang.demo1.pojo.Notice;
import com.quanfaqiang.demo1.pojo.Result;
import com.quanfaqiang.demo1.service.NoticeService;
import com.quanfaqiang.demo1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    NoticeService noticeService;

    @Autowired
    UserService userService;

    /**
     * 新增信息
     */
    @AddLog
    @PostMapping("/add")
    public Result add(@RequestBody Notice notice) {
        noticeService.add(notice);
        return Result.success("添加成功");
    }

    /**
     * 修改信息
     */
    @UpdateLog
    @PutMapping("/update")
    public Result update(@RequestBody Notice notice) {
        System.out.println(notice.getUpdateTime());
        noticeService.updateById(notice);
        return Result.success("修改成功");
    }

    /**
     * 删除信息
     */
    @DeleteLog
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        noticeService.removeById(id);
        return Result.success("删除成功");
    }


    /**
     * 批量删除信息
     */
    @DeleteLog
    @DeleteMapping("/deleteMore/{ids}")
    public Result batchDelete(@PathVariable List<Integer> ids) {
        noticeService.removeBatchByIds(ids);
        return Result.success("批量删除成功");
    }

    /**
     * 查询全部信息
     */

    @GetMapping("/selectAll")
    public Result selectAll(int currentPage, int pageSize) {
        System.out.println(currentPage+":"+pageSize+"-----------------------------------");
        Page<Notice> Page=noticeService.selectAll(currentPage,pageSize);
        return Result.success(Page);
    }




    @GetMapping("/serch")
    public Result serch(@RequestParam(defaultValue = "1")Integer currentPage, @RequestParam(defaultValue = "10") Integer pageSize,
                        String username, Integer state, String title,
                        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
                        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime){
        System.out.println(startTime);
        Page<Notice> Page=noticeService.serch(currentPage,pageSize,username,title,startTime,endTime,state);
        return Result.success(Page);
    }
}
